/**
 * i18n, 美版的mp3 download栏目, 共用js
 */
$(function() {
	mp3_index.run();
});

var mp3_index = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();
	},

	_init_ : function() {
		var path = location.pathname;		
		if(path.startWith(main.contextPath + "/world/")){			
			this.isI18n = true;			
		}		
	},

	_start_ : function() {
		this._shorten_music_title($("#mp3_list_div"));
	},

	_init_events : function() {
		$("#mprDownload_search").smenu({
			backend : 'fshared',
			defaultValue : 'Search for Music...',
			onchange : function(query) {			
				var sreg = /[`~!@#\$%\^&\*\(\)\+=\[\{\]\};:\<\>\?\/\\\|"]+/g;
				query = query.replace(sreg, "");
				if(mp3_index.isI18n){
					location.href = main.contextPath + "/world/music/" + query;		
				}else{
					location.href = main.contextPath + "/music/" + query;		
				}						
			}
		});	
		
		$("#page_box").pager({
			change: function(page){
				mp3_index._list_music(page);
			}
		});		
	},
	
	_list_music: function(page){
		var url = null;
		if(mp3_index.isI18n){
			url = main.contextPath + "/world/music/list/" + page + "?ajax=true";
		}else{
			url = main.contextPath + "/music/list/" + page + "?ajax=true";
		}
		$.get(url, function(result){
			var $div = $("<div>" + result + "</div>");
			mp3_index._shorten_music_title($div);
			$("#mp3_list_div").html($div.html());
		});
	},
	
	_shorten_music_title: function($div){
		$("ul > li > a", $div).each(function(){
			var $this = $(this);
			var txt = $this.text().replace('mp3', '');
			if(txt.length >= 30){
				txt = txt.substring(0, 25) + "...";
				$this.html(txt + " <span>mp3</span>");
			}			
		});
	}
};